/**
 * 
 */
package org.xlab.baseframework.test.service;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.xlab.baseframework.domain.ISystemUserDomain;
import org.xlab.baseframework.domain.SystemMessage;
import org.xlab.baseframework.exception.PermissionDeniedException;
import org.xlab.baseframework.exception.RootUserNotFoundException;
import org.xlab.baseframework.exception.SystemMessageAlreadyBeenReadException;
import org.xlab.baseframework.exception.SystemMessageNotFoundException;
import org.xlab.baseframework.service.api.ISystemMessageService;
import org.xlab.baseframework.service.impl.DefaultUserServiceImpl;
import org.xlab.baseframework.test.base.BaseTester;

/**
 * @author MaKuBEX
 * 
 */
@RunWith(BaseTester.class)
@ContextConfiguration("classpath:org/xlab/baseframework/conf/module/*/baseframework-*.xml")
public class SystemMessageServiceTestCase {
	private static final Logger log = Logger
			.getLogger(SystemMessageServiceTestCase.class);

	@Autowired
	@Qualifier("bpf_systemMessageService")
	private ISystemMessageService systemMessageService;

	@Autowired
	@Qualifier("bpf_defaultUserService")
	private DefaultUserServiceImpl systemUserService;

	@Test
	public void testCreateAndRead() throws SystemMessageNotFoundException,
			SystemMessageAlreadyBeenReadException, PermissionDeniedException,
			RootUserNotFoundException {
		ISystemUserDomain root = null;
		try {
			root = this.systemUserService.getSystemRootUser();
			this.systemUserService.createOrReplaceUser("user1", "user1",
					"User 1", "user1@abc.com");
			this.systemUserService.createOrReplaceUser("user2", "user2",
					"User 2", "user2@abc.com");
		} catch (RootUserNotFoundException e) {
			e.printStackTrace();
		}
		this.systemMessageService.createNotification("hello",
				this.systemUserService.getUser("user1").getUserCode(), root);
		List<String> destinationList = new ArrayList<String>();
		destinationList.add("user1");
		destinationList.add("user2");
		destinationList.add("user3");
		this.systemMessageService.createNotifications("Collection messages",
				destinationList, root);
		List<SystemMessage> allMessages = null;
		try {
			allMessages = this.systemMessageService.findAllByCreatedBy(root
					.getUserCode());
			assertEquals(4, allMessages.size());
			allMessages = this.systemMessageService
					.findAllByDestination("user1");
			assertEquals(2, allMessages.size());
			this.systemMessageService.markRead(allMessages.get(0).getId(),
					this.systemUserService.getUser("user2"));
		} catch (SystemMessageNotFoundException e) {
			e.printStackTrace();
		} catch (SystemMessageAlreadyBeenReadException e) {
			e.printStackTrace();
		} catch (PermissionDeniedException e) {
			log.debug("This was an expected exception! Because user2 can not mark user1's message as read!");
			this.systemMessageService.markRead(allMessages.get(0).getId(),
					this.systemUserService.getUser("user1"));
		}
		log.debug("Now root user is marking a message!");
//		String sn = allMessages.get(1).getSerialNumber();
		this.systemMessageService.markRead(allMessages.get(1).getId(), root);
		allMessages = this.systemMessageService.findAllByDestination("user1");
		assertEquals(2, allMessages.size());
	}
}
